<!DOCTYPE html>
<html>
<head>
    <%include("/back/_include/meta.html",{"title":""}){}%>
    <%include("/back/_include/style.html"){}%>
    <link href="/assets/ui/h+/css/plugins/iCheck/custom.css" rel="stylesheet">
</head>
<body class="gray-bg">
<div class="wrapper wrapper-content">
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-content">
                    <div class="row">
                        <div class="col-sm-12">
                            <form role="form" id="form">
                                <div class="form-group">
                                    <label>昵称</label>
                                    <input type="text" id="nickname" name="nickname" value="${admin.nickname!}"
                                           class="form-control" required="" minlength="2" maxlength="10">
                                    <span class="help-block m-b-none"><i class="fa fa-info-circle"></i> 2~10个字符以内</span>
                                </div>
                                <div class="text-left">
                                    <label>
                                        <input type="checkbox" id="modifyPwd" class="i-checks">
                                        修改密码
                                    </label>
                                </div>
                                <div id="passwords" class="hide">
                                    <div class="form-group">
                                        <label>原密码</label>
                                        <input type="password" id="oldPwd" name="oldPwd" class="form-control"
                                               required="" minlength="6" maxlength="16">
                                        <span class="help-block m-b-none"><i
                                                class="fa fa-info-circle"></i> 6~16个字符以内</span>
                                    </div>
                                    <div class="form-group">
                                        <label>新密码</label>
                                        <input type="password" id="password1" name="password1" class="form-control"
                                               required="" minlength="6" maxlength="16">
                                        <span class="help-block m-b-none"><i
                                                class="fa fa-info-circle"></i> 6~16个字符以内</span>
                                    </div>
                                    <div class="form-group">
                                        <label>重复新密码</label>
                                        <input type="password" id="password2" name="password2" class="form-control"
                                               required="" minlength="6" maxlength="16">
                                        <span class="help-block m-b-none"><i
                                                class="fa fa-info-circle"></i> 6~16个字符以内</span>
                                    </div>
                                </div>
                                <div>
                                    <button class="btn btn-lg btn-primary pull-left" id="save">
                                        <strong>更新</strong>
                                    </button>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<%include("/back/_include/script.html"){}%>
<script src="/assets/ui/h+/js/plugins/validate/jquery.validate.min.js"></script>
<script src="/assets/ui/h+/js/plugins/validate/messages_zh.min.js"></script>
<script src="/assets/ui/h+/js/plugins/iCheck/icheck.min.js"></script>
<script>
    $.validator.setDefaults({
        submitHandler: function () {
            var oldPwd = $$('#oldPwd')
            var password1 = $$('#password1')
            var password2 = $$('#password2')
            var nickname = $$('#nickname')
            var modifyPwd = $('#modifyPwd').prop("checked");
            var data;
            if(modifyPwd){
                data = {
                    "admin.id": "${admin.id!}",
                    "admin.nickname": nickname,
                    "admin.password": oldPwd,
                    "password1": password1,
                    "password2": password2,
                    "modifyPwd":modifyPwd
                }
            }else {
                data = {
                    "admin.id": "${admin.id!}",
                    "admin.nickname": nickname
                }
            }
            post("/b/admin/update", data, function (res) {
                if (!res.success) {
                    error(res.msg);
                    return;
                }
                console.log(res.data)
                if(res.data==true){
                    success("更新成功", false, function () {
                        window.parent.location.reload();
                    })
                    return
                }
                success("更新成功", false, function () {
                    window.location.reload();
                })
            }, function () {
                systemErr();
            })
        }
    });
    $(function () {
        var setting1 = {
            messages: {
                nickname: {required: "请输入昵称", minlength: "昵称必须2个字符以上"},
            }
        }
        var setting2 = {
            rules: {
                password2: {equalTo: "#password1"}
            },
            messages: {
                password1: {required: "请输入密码", minlength: "密码必须6个字符以上"},
                password2: {equalTo: "两次密码不一致"},
                nickname: {required: "请输入昵称", minlength: "昵称必须2个字符以上"},
            }
        }
        $("#form").validate(setting1)
        $('#modifyPwd').on('ifChecked',function () {
            $('#passwords').removeClass("hide")
            $("#form").validate(setting2)
        }).on('ifUnchecked',function () {
            $('#passwords').addClass("hide")
            $("#form").validate(setting1)
        })
        $(".i-checks").iCheck({checkboxClass: "icheckbox_square-green", radioClass: "iradio_square-green",})
    });
</script>
</body>
</html>